<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>FormatTime</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<style type="text/css">
<!--
.red {color: #FF0000}
-->
</style>
</head>
<body>

<h1>FormatTime</h1>

<p>Transforms a <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> timestamp into the specified date/time format.</p>

<pre class="Syntax">FormatTime, OutputVar [, <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a>, Format]</pre>
<h3>Parameters</h3>
<dl>

  <dt>OutputVar</dt>
  <dd><p>The name of the variable in which to store the result.</p></dd>

  <dt>YYYYMMDD...</dt>
  <dd><p>Leave this parameter blank to use the current local date and time. Otherwise, specify all or the leading part of a timestamp in the <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> format. If the date and/or time portion of the timestamp is invalid -- such as February 29th of a non-leap year -- the date and/or time will be omitted from <em>OutputVar</em>. Although only years between 1601 and 9999 are supported, a formatted time can still be produced for earlier years as long as the time portion is valid.</p></dd>

  <dt>Format</dt>
  <dd><p>If omitted, it defaults to the time followed by the long date, both of which will be formatted according to the current user's locale. For example: 4:55 PM Saturday, November 27, 2004</p>
      <p>Otherwise, specify one or more of the date-time formats below, along with any literal spaces and punctuation in between (commas do not need to be escaped; they can be used normally). In the following example, note that M must be capitalized: M/d/yyyy h:mm tt</p></dd>

</dl>

<h3>Date Formats <span class="red">(case sensitive)</span></h3>
<table class="info">
  <tr>
    <td>d</td>
    <td>Day of the month without leading zero (1 - 31)</td>
  </tr>
  <tr>
    <td>dd</td>
    <td>Day of the month with leading zero (01 &ndash; 31)</td>
  </tr>
  <tr>
    <td width="15%">ddd</td>
    <td width="85%">Abbreviated name for the day of the week (e.g. Mon) in the current user's language</td>
  </tr>
  <tr>
    <td>dddd</td>
    <td>Full name for the day of the week (e.g. Monday) in the current user's language</td>
  </tr>
  <tr>
    <td>M</td>
    <td>Month without leading zero (1 &ndash; 12)</td>
  </tr>
  <tr>
    <td>MM</td>
    <td>Month with leading zero (01 &ndash; 12)</td>
  </tr>
  <tr>
    <td>MMM</td>
    <td>Abbreviated month name (e.g. Jan) in the current user's language</td>
  </tr>
  <tr>
    <td>MMMM</td>
    <td>Full month name (e.g. January) in the current user's language</td>
  </tr>
  <tr>
    <td>y</td>
    <td>Year without century, without leading zero (0 &ndash; 99)</td>
  </tr>
  <tr>
    <td>yy</td>
    <td>Year without century, with leading zero (00 - 99)</td>
  </tr>
  <tr>
    <td>yyyy</td>
    <td>Year with century. For example: 2005</td>
  </tr>
  <tr>
    <td>gg</td>
    <td>Period/era string for the current user's locale (blank if none)</td>
  </tr>
</table>
<h3>Time Formats <span class="red">(case sensitive)</span></h3>
<table class="info">
  <tr>
    <td>h</td>
    <td>Hours without leading zero; 12-hour format (1 - 12)</td>
  </tr>
  <tr>
    <td>hh</td>
    <td>Hours with leading zero; 12-hour format (01 &ndash; 12)</td>
  </tr>
  <tr>
    <td width="15%">H</td>
    <td width="85%">Hours without leading zero; 24-hour format (0 - 23)</td>
  </tr>
  <tr>
    <td>HH</td>
    <td>Hours with leading zero; 24-hour format (00&ndash; 23)</td>
  </tr>
  <tr>
    <td>m</td>
    <td>Minutes without leading zero (0 &ndash; 59)</td>
  </tr>
  <tr>
    <td>mm</td>
    <td>Minutes with leading zero (00 &ndash; 59)</td>
  </tr>
  <tr>
    <td>s</td>
    <td>Seconds without leading zero (0 &ndash; 59)</td>
  </tr>
  <tr>
    <td>ss</td>
    <td>Seconds with leading zero (00 &ndash; 59)</td>
  </tr>
  <tr>
    <td>t</td>
    <td>Single character time marker, such as A or P (depends on locale)</td>
  </tr>
  <tr>
    <td>tt</td>
    <td>Multi-character time marker, such as AM or PM (depends on locale)</td>
  </tr>
</table>
<h3>The following formats must be used <span class="red">alone</span>; that is, with no other formats or text present in the <em>Format</em> parameter. These formats are not case sensitive.</h3>
<table class="info">
  <tr>
    <td>(Blank)</td>
    <td>Leave <em>Format</em> blank to produce the time followed by the long date. For example, in some locales it might appear as 4:55 PM Saturday, November 27, 2004</td>
  </tr>
  <tr>
    <td>Time</td>
    <td>Time representation for the current user's locale, such as 5:26 PM</td>
  </tr>
  <tr>
    <td width="15%">ShortDate</td>
    <td width="85%">Short date representation for the current user's locale, such as 02/29/04</td>
  </tr>
  <tr>
    <td>LongDate</td>
    <td>Long date representation for the current user's locale, such as Friday, April 23, 2004</td>
  </tr>
  <tr>
    <td>YearMonth</td>
    <td>Year and month format for the current user's locale, such as February, 2004</td>
  </tr>
  <tr>
    <td>YDay</td>
    <td>Day of the year without leading zeros (1 &ndash; 366)</td>
  </tr>
  <tr>
    <td>YDay0</td>
    <td>Day of the year with leading zeros (001 &ndash; 366)</td>
  </tr>
  <tr>
    <td>WDay</td>
    <td>Day of the week (1 &ndash; 7).  Sunday is 1.</td>
  </tr>
  <tr>
    <td>YWeek</td>
    <td>The ISO 8601 full year and week number. For example: 200453. If the week containing January 1st has four or more days in the new year, it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1. Consequently, both January 4th and the first Thursday of January are always in week 1.</td>
  </tr>
</table>
<h3>Additional Options</h3>
<p>The following options can appear inside the <em>YYYYMMDDHH24MISS</em> parameter immediately after the timestamp (if there is no timestamp, they may be used alone). In the following example, note the lack of commas between the last four items:</p>
<pre>FormatTime, OutputVar, 20040228 LSys D1 D4</pre>
<p><strong>R</strong>: Reverse. Have the date come before the time (meaningful only when <em>Format</em> is blank).</p>
<p><strong>Ln</strong>: If this option is <em>not</em> present, the current user's locale is used to format the string. To use the system's locale instead, specify LSys. To use a specific locale, specify the letter L followed by a hexadecimal or decimal locale identifier (LCID). For information on how to construct an LCID, search <a href="http://www.microsoft.com">www.microsoft.com</a> for the following phrase: Locale Identifiers</p>
<p><strong>Dn</strong>: Date options. Specify for <strong>n</strong> one of the following numbers:<br>
  0: Force the default options to be used. This also causes the short date to be in effect. <br>
  1: Use short date (meaningful only when <em>Format</em> is blank; not compatible with 2 and 8).<br>
  2: Use long date (meaningful only when <em>Format</em> is blank; not compatible with 1 and 8).<br>
  4: Use alternate calendar (if any).<br>
  8: Use Year-Month format (meaningful only when <em>Format</em> is blank; not compatible with 1 and 2).<br>
  0x10: Add marks for left-to-right reading order layout.<br>
  0x20: Add marks for right-to-left reading order layout.<br>
  0x80000000: Do not obey any overrides the user may have in effect for the system's default date format. <br>
0x40000000: Use the system ANSI code page for string translation instead of the locale's code page.</p>
<p><strong>Tn</strong>: Time options. Specify for <strong>n</strong> one of the following numbers:<br>
  0: Force the default options to be used. This also causes minutes and seconds to be shown.<br>
  1: Omit minutes and seconds.<br>
  2: Omit seconds. <br>
  4: Omit time marker (e.g. AM/PM).<br>
  8: Always use 24-hour time rather than 12-hour time.<br>
  12: Combination of the above two.<br>
  0x80000000: Do not obey any overrides the user may have in effect for the system's default time format. <br>
  0x40000000: Use the system ANSI code page for string translation instead of the locale's code page.</p>
<p><strong>Note</strong>: Dn and Tn may be repeated to put more than one option into effect, such as this example: <code>FormatTime, OutputVar, 20040228 D2 D4 T1 T8</code></p>
<h3 id="Remarks">Remarks</h3>
<p>Letters and numbers that you want to be transcribed literally from <em>Format</em> into <em>OutputVar</em> should be enclosed in single quotes as in this example: <code>'Date:' MM/dd/yy 'Time:' hh:mm:ss tt</code>.</p>
<p>By contrast, non-alphanumeric characters such as spaces, tabs, linefeeds (`n), slashes, colons, commas, and other punctuation do not need to be enclosed in single quotes. The exception to this is the single quote character itself: to produce it literally, use four consecutive single quotes (''''), or just two if the quote is already inside an outer pair of quotes.</p>
<p>If <em>Format</em> contains date and time elements together, they must not be intermixed. In other words, the string should be dividable into two halves: a time half and a date half. For example, a format string consisting of &quot;hh yyyy mm&quot; would not produce the expected result because it has a date element in between two time elements.</p>
<p>When <em>Format</em> contains a numeric day of the month (either d or dd) followed by the full month name (MMMM), the genitive form of the month name is used (if the language has a genitive form).</p>
<p>If <em>Format</em> contains more than 2000 characters, <em>OutputVar</em> will be made blank.</p>
<p>On a related note, addition and subtraction of dates and times can be performed with <a href="EnvAdd.htm">EnvAdd</a> and <a href="EnvSub.htm">EnvSub</a>.</p>
<h3>Related</h3>
<p>To convert in the reverse direction -- that is, <em>from</em> a formatted date/time <em>to</em> <a href="FileSetTime.htm#YYYYMMDD">YYYYMMDDHH24MISS</a> format -- see <a href="http://www.autohotkey.com/forum/topic20405.html">www.autohotkey.com/forum/topic20405.html</a></p>
<p>See also: <a href="GuiControls.htm#DateTime">Gui DateTime control</a>, <a href="SetFormat.htm">SetFormat</a>, <a href="Transform.htm">Transform</a>, <a href="../Variables.htm#date">built-in date and time variables</a>, <a href="FileGetTime.htm">FileGetTime</a></p>
<h3>Examples</h3>
<pre class="NoIndent">FormatTime, TimeString
MsgBox The current time and date (time first) is %TimeString%.

FormatTime, TimeString, R
MsgBox The current time and date (date first) is %TimeString%.

FormatTime, TimeString,, Time
MsgBox The current time is %TimeString%.

FormatTime, TimeString, T12, Time
MsgBox The current 24-hour time is %TimeString%.

FormatTime, TimeString,, LongDate
MsgBox The current date (long format) is %TimeString%.

FormatTime, TimeString, 20050423220133, dddd MMMM d, yyyy hh:mm:ss tt
MsgBox The specified date and time, when formatted, is %TimeString%.

FormatTime, TimeString, 200504, 'Month Name': MMMM`n'Day Name': dddd
MsgBox %TimeString%

FormatTime, YearWeek, 20050101, YWeek
MsgBox January 1st of 2005 is in the following ISO year and week number: %YearWeek%</pre>

<pre class="NoIndent"><em>; Change the date-time stamp of a file:</em>
FileSelectFile, FileName, 3,, Pick a file
if FileName =  <em>; The user didn't pick a file.</em>
    return
FileGetTime, FileTime, %FileName%
FormatTime, FileTime, %FileTime%   <em>; Since the last parameter is omitted, the long date and time are retrieved.</em>
MsgBox The selected file was last modified at %FileTime%.</pre>

<pre class="NoIndent"><em>; The following function converts the specified number of seconds into the corresponding
; number of hours, minutes, and seconds (hh:mm:ss format).</em>

MsgBox % FormatSeconds(7384)  <em>; 7384 = 2 hours + 3 minutes + 4 seconds. It yields: 2:03:04</em>

FormatSeconds(NumberOfSeconds)  <em>; Convert the specified number of seconds to hh:mm:ss format.</em>
{
    time = 19990101  <em>; *Midnight* of an arbitrary date.</em>
    time += %NumberOfSeconds%, seconds
    FormatTime, mmss, %time%, mm:ss
    return NumberOfSeconds//3600 &quot;:&quot; mmss
    <em>/*
    ; Unlike the method used above, this would not support more than 24 hours worth of seconds:
    FormatTime, hmmss, %time%, h:mm:ss
    return hmmss
    */</em>
}</pre>

</body>
</html>
